package cn.iocoder.yudao.module.biz.controller.admin.licencePerson;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.biz.controller.admin.licencePerson.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.licencePerson.BizLicencePersonDO;
import cn.iocoder.yudao.module.biz.convert.licencePerson.BizLicencePersonConvert;
import cn.iocoder.yudao.module.biz.service.licencePerson.BizLicencePersonService;

@Api(tags = "管理后台 - 作业人员")
@RestController
@RequestMapping("/biz/licence-person")
@Validated
public class BizLicencePersonController {

    @Resource
    private BizLicencePersonService licencePersonService;

    @PostMapping("/create")
    @ApiOperation("创建作业人员")
    @PreAuthorize("@ss.hasPermission('biz:licence-person:create')")
    public CommonResult<String> createLicencePerson(@Valid @RequestBody BizLicencePersonCreateReqVO createReqVO) {
        return success(licencePersonService.createLicencePerson(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新作业人员")
    @PreAuthorize("@ss.hasPermission('biz:licence-person:update')")
    public CommonResult<Boolean> updateLicencePerson(@Valid @RequestBody BizLicencePersonUpdateReqVO updateReqVO) {
        licencePersonService.updateLicencePerson(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除作业人员")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:licence-person:delete')")
    public CommonResult<Boolean> deleteLicencePerson(@RequestParam("id") String id) {
        licencePersonService.deleteLicencePerson(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得作业人员")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:licence-person:query')")
    public CommonResult<BizLicencePersonRespVO> getLicencePerson(@RequestParam("id") String id) {
        BizLicencePersonDO licencePerson = licencePersonService.getLicencePerson(id);
        return success(BizLicencePersonConvert.INSTANCE.convert(licencePerson));
    }

    @GetMapping("/list")
    @ApiOperation("获得作业人员列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:licence-person:query')")
    public CommonResult<List<BizLicencePersonRespVO>> getLicencePersonList(@RequestParam("ids") Collection<String> ids) {
        List<BizLicencePersonDO> list = licencePersonService.getLicencePersonList(ids);
        return success(BizLicencePersonConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得作业人员分页")
    @PreAuthorize("@ss.hasPermission('biz:licence-person:query')")
    public CommonResult<PageResult<BizLicencePersonRespVO>> getLicencePersonPage(@Valid BizLicencePersonPageReqVO pageVO) {
        PageResult<BizLicencePersonDO> pageResult = licencePersonService.getLicencePersonPage(pageVO);
        return success(BizLicencePersonConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出作业人员 Excel")
    @PreAuthorize("@ss.hasPermission('biz:licence-person:export')")
    @OperateLog(type = EXPORT)
    public void exportLicencePersonExcel(@Valid BizLicencePersonExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BizLicencePersonDO> list = licencePersonService.getLicencePersonList(exportReqVO);
        // 导出 Excel
        List<BizLicencePersonExcelVO> datas = BizLicencePersonConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "作业人员.xls", "数据", BizLicencePersonExcelVO.class, datas);
    }

}
